﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using HYView;

namespace YJKArch.LinearArrayModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdLinearArray : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			try
			{
				Process currentProcess = Process.GetCurrentProcess();
				if (currentProcess != null)
				{
					WindowHandle owner = new WindowHandle(currentProcess.MainWindowHandle);
					try
					{
						bool flag = true;
						while (flag)
						{
							DlgLinearArray instance = DlgLinearArray.GetInstance(cmdData);
							if (instance.ShowDialog(owner) != DialogResult.OK)
							{
								break;
							}
							Transaction transaction = new Transaction(this.m_uiApp.ActiveUIDocument.Document, "CreateInstance");
							try
							{
								transaction.Start();
								flag = instance.Redo;
								if (instance.PlaceType == PlaceType.PickElement)
								{
									Element element = null;
									try
									{
										element = this.m_doc.GetElement(this.m_uiApp.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "请选择阵列对象。"));
									}
									catch
									{
										MessageBox.Show("没有选择阵列对象。", "盈建科提示");
										transaction.RollBack();
										continue;
									}
									using (List<IntersectionPoint>.Enumerator enumerator = GeoUtil.GetLinkCurve(cmdData.Application).Divide((double)(instance.Num + 1), cmdData.Application).GetEnumerator())
									{
										while (enumerator.MoveNext())
										{
											IntersectionPoint intersectionPoint = enumerator.Current;
											try
											{
												XYZ xyz = null;
												if (element.Location is LocationCurve)
												{
													if ((element.Location as LocationCurve).Curve is Line)
													{
														xyz = LineAssistFunc.Midpoint((element.Location as LocationCurve).Curve as Line);
													}
													else if ((element.Location as LocationCurve).Curve is Arc)
													{
														xyz = ((element.Location as LocationCurve).Curve as Arc).Center;
													}
												}
												else if (element.Location is LocationPoint)
												{
													xyz = (element.Location as LocationPoint).Point;
												}
												double num = instance.Angle;
												if (instance.AutoRotation)
												{
													num += intersectionPoint.Tangential;
												}
												ElementTransformUtils.RotateElements(this.m_doc, ElementTransformUtils.CopyElement(this.m_doc, element.Id, intersectionPoint.Point - xyz), YJKLineEx.YJKGetUnBound(intersectionPoint.Point, XYZ.BasisZ), num);
											}
											catch
											{
											}
										}
										goto IL_309;
									}
								}
								if (instance.HostCode == 0)
								{
									using (List<IntersectionPoint>.Enumerator enumerator = GeoUtil.GetLinkCurve(cmdData.Application).Divide((double)(instance.Num + 1), cmdData.Application).GetEnumerator())
									{
										while (enumerator.MoveNext())
										{
											IntersectionPoint intersectionPoint2 = enumerator.Current;
											try
											{
												double num2 = instance.Angle;
												if (instance.AutoRotation)
												{
													num2 += intersectionPoint2.Tangential;
												}
												UnifiedModified.ActivateFamilySymbol(instance.FamilySymbol);
												ElementTransformUtils.RotateElement(this.m_doc, this.m_doc.Create.NewFamilyInstance(intersectionPoint2.Point, instance.FamilySymbol, AssistFunc.GetCurrentViewLevel(this.m_uiDoc.Document), (Autodesk.Revit.DB.Structure.StructuralType)0).Id, YJKLineEx.YJKGetBound(intersectionPoint2.Point, XYZ.BasisZ), num2);
											}
											catch
											{
											}
										}
										goto IL_309;
									}
								}
								MessageBox.Show("请选择不基于主体的族。", "盈建科提示");
								IL_309:
								transaction.Commit();
							}
							catch
							{
								transaction.RollBack();
							}
						}
						goto IL_348;
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "盈建科提示");
						goto IL_348;
					}
				}
				MessageBox.Show("未能得到当前Revit进程！", "盈建科提示");
				IL_348:;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示");
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
